package com.netty.source_code.memory_pool;

import io.netty.util.Recycler;

/**
 * @author xl-9527
 * @since 2025/7/19
 **/
public class RecyclerDemo {

    public static void main(String[] args) {
        final Recycler<User> recycler = new Recycler<>() {
            @Override
            protected User newObject(final Handle<User> handle) {
                return new User(handle);
            }
        };

        final User user = recycler.get();
        user.name = "xl-9527";
        user.release();
        System.out.println(recycler.get().name);
    }

    private static class User {
        private String name;

        private final Recycler.Handle<User> handle;

        public User(Recycler.Handle<User> handle) {
            this.handle = handle;
        }

        public void release() {
            handle.recycle(this);
        }
    }
}
